﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ERP.Classes;
using ERP.View;

namespace ERP.Khachhang
{
    public partial class viewERP_SodoKhachhang : DevExpress.XtraEditors.XtraForm
    {
        public cs_erp_hoadonInfo hoadon = new cs_erp_hoadonInfo();
        public cs_hmr_usersInfo user = new cs_hmr_usersInfo();
        public cs_erp_khachhangInfo kh = new cs_erp_khachhangInfo();
        public cs_erp_khachhang_sodoInfo hh = new cs_erp_khachhang_sodoInfo();
        int count = 0;
        public int flag = 1;

        public viewERP_SodoKhachhang()
        {
            InitializeComponent();
        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            viewErp_Ql_Khachhang kh = new viewErp_Ql_Khachhang();
            kh.user = this.user;
            kh.ShowDialog();
            if (kh.flag == 4)
            {
                this.kh = kh.khachhang;
                this.Load_Info();
                this.flag = 2;
                this.Load_Data();
            }
        }

        private void Load_Info()
        {
            this.txtHoten.Text = this.kh.tenkhachhang;
            this.txtDiachi.Text = this.kh.diachi;
            this.txtDienthoai.Text = this.kh.dienthoai;
        }

        private void Load_Data()
        {
            DataSet ds = new DataSet("ds");
            this.hh.id_khachhang = this.kh.id_kh;
            if (this.flag == 3)
            {
                ds = this.hh.Get_List_Nhapsodo_Theo_HD(this.hoadon.id_hd);
            }
            else
            {
                ds = this.hh.Get_List();
            }
            if (ds == null || ds.Tables[0].Rows.Count == 0)
            {
                this.count = 0;
            }
            else
            {
                this.count = ds.Tables[0].Rows.Count;
                this.grcDanhsach.DataSource = ds.Tables[0];
                this.gridView1.ExpandAllGroups();
            }
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (this.flag == 1)
            {
                XtraMessageBox.Show("Bạn phải chọn khách hàng trước khi thực hiện!", "Thông báo...");
                return;
            }
            this.hh.id_khachhang = this.kh.id_kh;
            int k = 0;
            for (int i = 0; i < this.count; i++)
            {
                this.hh.id_thuoctinh = int.Parse(this.gridView1.GetRowCellValue(i, grdcID).ToString().Trim());
                this.hh.id_hh = int.Parse(this.gridView1.GetRowCellValue(i,grdcMaql).ToString().Trim());
                this.hh.giatri = this.gridView1.GetRowCellValue(i,grdcDvt).ToString().Trim();
                this.hh.ghichu = this.gridView1.GetRowCellValue(i, grdcDongia).ToString().Trim();
                this.hh.ngaynhap = DateTime.Now;
                int kk = 0;
                kk = this.hh.ExcuteProcedure(2);
                if (kk <= 0)
                {
                    kk = this.hh.ExcuteProcedure(1);
                }
                k += kk;
            }
            XtraMessageBox.Show("Lưu thành công: " + k + " số đo của khách hàng!", "Thông báo...");
            this.Dispose(true);
        }

        private void viewERP_SodoKhachhang_Load(object sender, EventArgs e)
        {
            if (this.flag == 2 || this.flag == 3)
            {
                this.Load_Info();
                this.Load_Data();
            }
            this.CheckUser();
        }
        private void CheckUser()
        {
            cs_hmr_userrightInfo userright = new cs_hmr_userrightInfo();
            userright.groupid = this.user.groupid;
            cs_hmr_winformInfo wf = new cs_hmr_winformInfo();
            wf.tenform = this.Name;
            if (wf.Get_Info() > 0)
            {
                userright.form_id = wf.formid;
                userright.Get_Info();
                if (userright.savedata == 0)
                {
                    this.btnLuu.Enabled = false;
                }
                if (userright.deletedata == 0)
                {
                }
                if (userright.updatedata == 0)
                {
                    this.btnLuu.Enabled = false;
                }
                if (userright.printreport == 0)
                {
                }
            }
        }
        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}